package com.sc.VenusQueryBeta.presenter.account.cunka;

import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.sc.VenusQueryBeta.contract.account.cunka.CunKa02Contract;
import com.sc.VenusQueryBeta.model.account.cunka.CunKa02Model;
import com.sc.VenusQueryBeta.model.bean.account.CurrencyListBean;
import com.sc.VenusQueryBeta.model.bean.account.cunka.CunKaList2;

import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 18/10/2018.
 *
 */

public class CunKa02Presenter extends CunKa02Contract.CunKa02Presenter {
    private int mCount = 10;
    private int mStart;
    private boolean isLoading;
    public static CunKa02Presenter newInstance() {
        return new CunKa02Presenter();
    }

    @Override
    protected CunKa02Contract.ICunKa02ActivitynModel getModel() {
        return CunKa02Model.newInstance();
    }

    @Override
    public void onStart() {

    }

    /**
     * 第一次加載
     * @param serverAddress
     * @param projectName
     * @param serverPort
     * @param clientindex
     * @param clientAppmsgnum
     */
    @Override
    public void getCunKa02(String serverAddress, String projectName, int serverPort,int clientindex, int startint ,int limit,int roomid,
                           String clientAppmsgnum,String currencynameCN) {
        if (mIView == null || mIModel == null)
            return;
        mStart = 0;
        mRxManager.register(mIModel.getCunKa02(serverAddress,projectName, serverPort,clientindex,mStart,mCount, roomid,clientAppmsgnum,currencynameCN).subscribe(new Consumer<CunKaList2>() {
            @Override
            public void accept(CunKaList2 cunKaList2Bean) throws Exception {
                if (mIView != null)
                    mStart += mCount;
                    mIView.showCunKaData2Success(cunKaList2Bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
//                    if (mIView.isVisiable())
                    LogUtils.e("第一次加載 error=="+throwable.getMessage());
                    mIView.showToast("Network error");
                    mIView.showNetworkError(throwable);
                }
            }
        }));
    }

    @Override
    public void queryAllCurrencyList(String serverAddress, String projectName, int serverPort) {
        if (mRxManager != null && mIModel != null) {
            mRxManager.register(mIModel.queryAllCurrencyList(serverAddress, projectName, serverPort).subscribe(new Consumer<CurrencyListBean>() {
                @Override
                public void accept(CurrencyListBean currencyListBean) throws Exception {
                    mIView.showqueryAllCurrencyList(currencyListBean);
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    LogUtils.e("Network error." + throwable.getMessage());
//                if (mIView != null)
//                    return;
                    mIView.showToast("Network error.");
                    mIView.showNetworkError(throwable);
                }
            }));
        }else {

        }
    }

    /**
     * 加載更多
     * @param serverAddress
     * @param projectName
     * @param serverPort
     * @param clientindex
     * @param clientAppmsgnum
     */
    @Override
    public void getMoreCunKa02(String serverAddress, String projectName, int serverPort, int clientindex, int roomid, String clientAppmsgnum,String currencynameCN) {
        if (!isLoading) {
            isLoading = true;
            //一次加载 条数据
            mRxManager.register(mIModel.getCunKa02(serverAddress,projectName, serverPort,clientindex,mStart,mCount,roomid,clientAppmsgnum,currencynameCN).subscribe(new Consumer<CunKaList2>() {
                @Override
                public void accept(CunKaList2 cunKaList2Bean) throws Exception {
                    isLoading = false;
                    if (mIView == null)
                        return;
                    if (cunKaList2Bean != null && cunKaList2Bean.getList() != null && cunKaList2Bean.getList().size() > 0) {
                        mStart += mCount;
                        LogUtils.e("mStart1==" + mStart);
                        mIView.showCunKaData2Success(cunKaList2Bean);
                        //缓存数据

                    } else {
                        //否则显示没有加载更多数据
                        mIView.showNoMoreData();
                    }

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading = false;
                    if (mIView != null) {
//                    if (mIView.isVisiable())
                        LogUtils.e("加載更多 error=="+throwable.getMessage());
                        mIView.showToast("Network error");
                        mIView.showNetworkError(throwable);
                    }
                }
            }));
        }
    }
}
